using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
	public class SectionRepository : ISectionRepository, IDisposable
	{
		private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

		public SectionRepository(dbVoiceAdDataContext db)
		{
			this.context = db;
		}

		public IEnumerable<tblSection>GetListSections()
		{
			return context.tblSections.ToList();
		}

		public tblSection GetSectionById(int id)
		{
			return context.tblSections.Where(x => x.Id == id).FirstOrDefault();
		}

		public void InsSection(tblSection tblsection)
		{
			context.tblSections.InsertOnSubmit(tblsection);
			context.SubmitChanges();
		}

		public void UpdSection(tblSection tblsection)
		{
			tblSection obj =  context.tblSections.Where(x => x.Id == tblsection.Id).First();
			obj.SectionName_Vn = tblsection.SectionName_Vn;
			obj.SectionName_En = tblsection.SectionName_En;
			context.SubmitChanges();
		}

		public void DelSection(int id)
		{
			tblSection obj =  context.tblSections.Single(x => x.Id == id);
			context.tblSections.DeleteOnSubmit(obj);
			context.SubmitChanges();
		}

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
	}
}
